$(function () {
    var layer = layui.layer;
    var form = layui.form;
    initCateList()
    //----------------------------------------------------------------
    // 添加新楼栋dialog
    var addCate_index = null;
    $('#addCate').on('click', function () {
        addCate_index = layer.open({
            type: 1,
            area: ['400px', ''],
            title: '添加楼栋',
            content: $('#tpl_dialog_addCate').html(),
            success: function () {
                form.render("select");
                initGetAdmin()
                initGetLou()
            }
        })
    })
    //----------------------------------------------------------------
    // 添加楼栋
    $('body').on('submit', '#form_addCate', function (e) {
        e.preventDefault();
        var cate_name = $('#form_addCate [name = cate_name]').val();
        var lou_type = $('#form_addCate [name = lou_type]').val();
        var admin_user = $('#form_addCate [name = admin_user]').val();
        var phone = $('#form_addCate [name = phone]').val();
        // console.log(cate_name);
        // console.log(lou_type);
        // console.log(admin_user);
        // console.log(phone);
        $.ajax({
            method: 'POST',
            url: '/my/deviceCate/addDeviceCate',
            data: {
                cate_name: cate_name,
                lou_type: lou_type,
                admin_user: admin_user,
                phone: phone
            },
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                initCateList();
                layer.msg('添加楼栋成功');
                layer.close(addCate_index);
            }
        })
    })
    //----------------------------------------------------------------
    // 获取全部楼栋
    function initCateList() {
        $.ajax({
            method: 'GET',
            url: '/my/deviceCate/deviceCateList',
            success: function (res) {
                var htmlStr = template('tpl_cateList', res);
                $('tbody').html(htmlStr);

            }
        })
    }
    //----------------------------------------------------------------
    // 根据id获取楼栋
    var editCate_index = null;
    $('tbody').on('click', '.btn_editCate', function () {
        editCate_index = layer.open({
            type: 1,
            area: ['400px', ''],
            title: '修改楼栋信息',
            content: $('#tpl_dialog_editCate').html(),
            success: function () {
                form.render("select");
                initGetAdmin()

            }
        })
        var id = $(this).attr('data_id');
        // console.log(id);
        $.ajax({
            method: 'GET',
            url: '/my/deviceCate/deviceCateList/' + id,
            success: function (response) {
                // console.log(response.data);
                form.val('form_edit', response.data[0])
            }
        })
    })
    // 根据id修改楼栋
    $('body').on('submit', '#form_editCate', function (e) {
        e.preventDefault();
        // console.log('ok');
        const id = $('#form_editCate [name=id]').val();
        const cate_name = $('#form_editCate [name = cate_name]').val();
        const lou_type = $('#form_editCate [name = lou_type]').val();
        const admin_user = $('#form_editCate [name = admin_user]').val();
        const phone = $('#form_editCate [name = phone]').val();
        // console.log(lou_type);
        $.ajax({
            method: 'POST',
            url: '/my/deviceCate/editDeviceCate',
            data: {
                id: id,
                cate_name: cate_name,
                lou_type: lou_type,
                admin_user: admin_user,
                phone: phone
            },
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('修改楼栋信息失败！')
                }
                initCateList();
                layer.msg('修改楼栋信息成功');
                layer.close(editCate_index);
            }
        })
    })
    // 根据id删除楼栋
    $('tbody').on('click', '.btn_deleteCate', function () {
        var id = $(this).attr('data_id');
        layer.confirm('确定删除此楼栋？', {
            icon: 3,
            title: '提示'
        }, function (deleteCate_index) {
            // console.log(id);
            $.ajax({
                method: 'GET',
                url: '/my/deviceCate/deleteDeviceCate/' + id,
                success: function (res) {
                    // console.log(res);
                    if (res.status !== 0) {
                        layer.close(deleteCate_index)
                        return layer.msg(res.message);
                    }
                    layer.msg("删除楼栋成功");
                    initCateList();
                    layer.close(deleteCate_index)
                }
            })
        });
    });

    // 获取宿舍管理员分类列表数据的方法
    function initGetAdmin() {
        $.ajax({
            method: 'GET',
            url: '/my/userinfo/getAdminList/',
            success: function (res) {
                // console.log(res);
                if (res.status !== 0) {
                    return layer.msg('获取宿舍管理员分类数据失败！');
                }
                // console.log(res.data);
                // 调用模板引擎渲染分类可选项
                var chtmlStr = template('tpl_dialog_addCate', res.data);
                // console.log(chtmlStr);
                $('[name = admin_user]').html(chtmlStr);
                // 通过 layui 重新渲染表单区域的UI结构
                form.render("select");
            }
        });
    }
    // 获取楼栋分类列表数据的方法
    function initGetLou() {
        // 调用模板引擎渲染分类可选项
        var lou_type = [{
            lou_type: '男生楼'
        }, {
            lou_type: '女生楼'
        }]
        var chtmlStr = template('tpl_dialog_addCate', lou_type);
        // console.log(chtmlStr);
        $('[name = lou_type]').html(chtmlStr);
        // 通过 layui 重新渲染表单区域的UI结构
        form.render("select");
    }
})